-- This script was generated by the Schema Diff utility in pgAdmin 4
-- For the circular dependencies, the order in which Schema Diff writes the objects is not very sophisticated
-- and may require manual changes to the script to ensure changes are applied in the correct order.
-- Please report an issue for any failure with the reproduction steps.

CREATE OR REPLACE VIEW public.model_metrics
    AS
     SELECT m.model,
    sum(m.total_tokens) AS sum_tokens,
    sum(m.prompt_tokens) AS sum_prompt_tokens,
    sum(m.completion_tokens) AS sum_completion_tokens,
    count(*) AS request_count
   FROM ( SELECT response.id,
            response.created_at,
            response.body,
            response.request,
            response.body ->> 'model'::text AS model,
            ((response.body -> 'usage'::text) ->> 'total_tokens'::text)::bigint AS total_tokens,
            ((response.body -> 'usage'::text) ->> 'prompt_tokens'::text)::bigint AS prompt_tokens,
            ((response.body -> 'usage'::text) ->> 'completion_tokens'::text)::bigint AS completion_tokens
           FROM response_rbac response) m
  GROUP BY m.model;
